package com.dora.spring.cluster.connect;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

import java.util.HashSet;
import java.util.Set;

public class SentinelTest {
    public static void main(String[] args) {
        // 哨兵节点的地址
        Set<String> sentinels = new HashSet<>();
        sentinels.add("192.168.15.128:26379");
        sentinels.add("192.168.15.129:26379");
        sentinels.add("192.168.15.130:26379");

        // 设置密码
        String password = "your_master_password";

        // 创建 JedisSentinelPool 对象，并传入密码
        JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels, password);

        // 获取 Jedis 实例
        try (Jedis jedis = pool.getResource()) {
            // 测试连接
            jedis.set("test-key", "Hello, Sentinel with Password!");
            String value = jedis.get("test-key");
            System.out.println("The value of 'test-key' is: " + value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭连接池
            pool.close();
        }
    }
}
